package com.cisdi.analysis.service.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author liujunnan
 * @date 2018/5/23
 */
@Component
public class QcloudClient {

    /**
     * 超过这个大小腾讯云会使用分片上传，这里没写
     */
    private final static String FILE_SIZE = "file.size";


    private final static String SECRET_ID = "secret.id";


    private final static String SECRET_KEY = "secret.key";


    private final static String BUCKET_NAME = "bucketName";

    @Autowired
    private Environment env;

    /**
     * 自动判断文件的大小,已使用是否是分片的方式上传文件
     * @param remoteFolderPath 远程文件夹的名称
     * @param loaclPath        文件的本地路径
     * @return
     */
    public String uploadFile(String remoteFolderPath, String loaclPath) throws Exception {

        //获取文件输入流
        File f = new File(loaclPath);
        String result = null;
        if (f.exists() && f.isFile()) {
            long size = f.length();
            //小于等于20M
            if (size <= Integer.parseInt(env.getProperty(FILE_SIZE))) {
                String remotePath = new StringBuilder(remoteFolderPath).append(getFileName(loaclPath)).toString();
                COSCredentials cred = new BasicCOSCredentials(env.getProperty(SECRET_ID), env.getProperty(SECRET_KEY));
                ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
                COSClient cosclient = new COSClient(cred, clientConfig);
                PutObjectRequest putObjectRequest = new PutObjectRequest(env.getProperty(BUCKET_NAME), remotePath, f);
                PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
                System.out.println("uploadFile result:" + putObjectResult.toString());
                return putObjectResult.toString();
            } else {
                //没写分片,因为大小就4MB
                throw new Exception("文件长度太长，上传失败");
            }
        } else {
            System.out.println("file doesn't exist or is not a file");
        }
        return result;
    }

    /**
     * 上传一个长度为10个字节的文件
     * @param remotePath
     * @param loaclPath
     * @return
     */
    private String cosUploadFile(String remotePath, File f) {
        COSCredentials cred = new BasicCOSCredentials(env.getProperty(SECRET_ID), env.getProperty(SECRET_KEY));
        ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
        COSClient cosclient = new COSClient(cred, clientConfig);
        PutObjectRequest putObjectRequest = new PutObjectRequest(env.getProperty(BUCKET_NAME), remotePath, f);
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        System.out.println("uploadFile result:" + putObjectResult.toString());
        return putObjectResult.toString();
    }

    /**
     * 根据本地文件的路径获取文件的名字
     * @param localPath 本地文件路径
     * @return 文件的名称
     */
    private String getFileName(String localPath) {
        String[] result = localPath.split("\\/");
        return result[result.length - 1];
    }
}
